﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Medival_Snake
{
    public enum ChaseDirY
    {
        ChaseUp,
        ChaseDown,
        ChaseNone
    }

    public enum ChaseDirX
    {
        ChaseLeft,
        ChaseRight,
        ChaseNone
    }

    class PathFinder
    {
        Objects NPC;

        public ChaseDirY DirY = ChaseDirY.ChaseNone;

        public ChaseDirX DirX = ChaseDirX.ChaseNone;

        bool SwitchX = false;
        bool SwitchY = false;

        public PathFinder(Objects NPC)
        {
            this.NPC = NPC;
        }

        public bool NpcIsRightFromPlayer()
        {
            if (NPC.Position.X > Game1.Player.Position.X)
                return true;
            else
                return false;
        }

        public bool NpcIsDownFromPlayer()
        {
            if (NPC.Position.Y > Game1.Player.Position.Y)
                return true;
            else
                return false;
        }

        public void PathCollider() //Set Actions for WayPoints
        {
            //*************************************************************
            for (int i = 0; i < Game1.Lvl1.WayPointsUp.Count; i++ ) //up Wp
            {
                
                if (Game1.Lvl1.WayPointsUp[i].Intersects(NPC.Getbounds()))
                {
                    if (NpcIsDownFromPlayer())
                    {
                        DirY = ChaseDirY.ChaseUp;
                    }
                    else
                    {
                        //do Nothing
                    }

                    if (NpcIsRightFromPlayer())
                    {
                        //do Nothing
                    }
                    else
                    {
                        //do Nothing
                    }

                }
            }
            //*************************************************************
            for (int i = 0; i < Game1.Lvl1.WayPointsDown.Count; i++) //´Down Wp
            {
                if (Game1.Lvl1.WayPointsDown[i].Intersects(NPC.Getbounds()))
                {

                    if (NpcIsDownFromPlayer())
                    {
                        //do Nothing
                    }
                    else
                    {
                        DirY = ChaseDirY.ChaseDown;
                    }

                    if (NpcIsRightFromPlayer())
                    {
                        //do Nothing
                    }
                    else
                    {
                        //do Nothing
                    }

                }
            }
            //*************************************************************
            for (int i = 0; i < Game1.Lvl1.WayPointsLeft.Count; i++) //Left Wp
            {
                if (Game1.Lvl1.WayPointsLeft[i].Intersects(NPC.Getbounds()))
                {

                    if (NpcIsDownFromPlayer())
                    {
                        //do Nothing
                    }
                    else
                    {
                        //do Nothing
                    }

                    if (NpcIsRightFromPlayer())
                    {
                        DirX = ChaseDirX.ChaseLeft;
                    }
                    else
                    {
                        //do Nothing
                    }

                }
            }
            //*************************************************************
            for (int i = 0; i < Game1.Lvl1.WayPointsRight.Count; i++) //Right Wp
            {
                if (Game1.Lvl1.WayPointsRight[i].Intersects(NPC.Getbounds()))
                {

                    if (NpcIsDownFromPlayer())
                    {
                        //do Nothing
                    }
                    else
                    {
                        //do Nothing
                    }

                    if (NpcIsRightFromPlayer())
                    {
                        //do Nothing
                    }
                    else
                    {
                        DirX = ChaseDirX.ChaseRight;
                    }

                }
            }
            //*************************************************************
            for (int i = 0; i < Game1.Lvl1.WayPointsAllDir.Count; i++) //All Dir Wp
            {
                if (Game1.Lvl1.WayPointsAllDir[i].Intersects(NPC.Getbounds()))
                {

                    if (NpcIsDownFromPlayer())
                    {
                        if (SwitchX == false)
                        {
                            DirY = ChaseDirY.ChaseUp;
                            SwitchY = true;
                        }
                    }
                    else
                    {
                        if (SwitchX == false)
                        {
                            DirY = ChaseDirY.ChaseDown;
                            SwitchY = true;
                        }
                    }

                    if (NpcIsRightFromPlayer())
                    {
                        if (SwitchY == false)
                        {
                            DirX = ChaseDirX.ChaseLeft;
                            SwitchX = true;
                        }
                    }
                    else
                    {
                        if (SwitchY == false)
                        {
                            DirX = ChaseDirX.ChaseRight;
                            SwitchX = true;
                        }
                    }

                    SwitchX = false;
                    SwitchY = false;

                }
            }
        }//end All Actions

        public string MoveDir()
        {
            switch (DirX)
            {
                case ChaseDirX.ChaseLeft:

                    return "MoveLeft";

                case ChaseDirX.ChaseRight:

                   return "MoveRight";
            }
            switch (DirY)
            {
                case ChaseDirY.ChaseUp:

                    return "MoveUp";

                case ChaseDirY.ChaseDown:

                    return "MoveDown";
            }

            return "MoveNone";

        }

        public Vector2 SetSpeed()
        {

            NPC.ResetMove();

            string md = MoveDir();

            float xSpeed = new float();
            float ySpeed = new float();

            //****Y-axel Factors.****
            if (NpcIsDownFromPlayer())
            { ySpeed = NPC.Move.Y *= -2; }

            if (NpcIsDownFromPlayer() == false)
            { ySpeed = NPC.Move.Y *= 2; }

            //****X-axel Factors.****
            if (NpcIsRightFromPlayer())
            { xSpeed = NPC.Move.X *= -2; }

            if (NpcIsRightFromPlayer() == false)
            { xSpeed = NPC.Move.X *= 2; }

            if (md == "MoveRight" || md == "MoveLeft")
                ySpeed = 0;
            if (md == "MoveUp" || md == "MoveDown")
                xSpeed = 0;

            return new Vector2(xSpeed,ySpeed);
        }
    }
}
